package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class PagePopupAdapter extends RecyclerView.Adapter<PagePopupAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private String[] str;
    private int mSelectedItem = -1;

    public PagePopupAdapter(Context context, String[] array){
        mContext = context;
        str = array;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page_pop,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String label = str[position];
        holder.radioButton.setText(label);
        holder.radioButton.setId(position);
        holder.radioButton.setClickable(position == mSelectedItem);

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(str[position],position);
        });
    }

    @Override
    public int getItemCount() {
        return str == null? 0 : str.length;
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        public RadioButton radioButton;
        public ViewHolder(View itemView){
            super(itemView);
            radioButton = (RadioButton) itemView.findViewById(R.id.tv_name);
            View.OnClickListener clickListener = new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    mSelectedItem = getAdapterPosition();
                    notifyItemRangeChanged(0, str.length);
                }
            };
            itemView.setOnClickListener(clickListener);
            radioButton.setOnClickListener(clickListener);
        }
    }

    public void addDate(String[] strings){
        str = strings;
        notifyDataSetChanged();
    }


    public interface OnItemClickListenter {
        void itemClickListenter(String scan, int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

}
